#include <stdio.h>
#include <stdlib.h>

int Magician_array(int arr[], int size) {
    arr[0] = 1; // 第一个位置为1

    int i, j, count, curr;
    curr = 0;

    for (i = 1; i < size; i++) { // i从第2个开始
        count = i + 1;

        for (j = 0; j < count; j++) {
            curr = (curr + 1);
            if (curr == size) {
                curr = 0;
            }

            if (arr[curr] != 0) {
                count++;
            }
        }

        if (0 == arr[curr]) {
            arr[curr] = i + 1;
        }
    }

    return 0;
}

int main() {
    const int cardsNum = 13;

    // calloc在动态分配完内存后，自动初始化该内存空间为零，而malloc不初始化，里边数据是随机的垃圾数据。
    int* arr = (int*)calloc(cardsNum, sizeof(int));
    if (!arr) {
        printf("calloc err\n");
        return -1;
    }
    int i = 0;

    Magician_array(arr, cardsNum);

    for (; i < cardsNum; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    if (arr) {
        free(arr);
        arr = NULL;
    }
    return 0;
}
